package com.woniu.woniuproject.service;

import com.woniu.woniuproject.entity.Account;
import com.woniu.woniuproject.entity.Account_log;
import com.woniu.woniuproject.entity.LooseLabel;
import com.woniu.woniuproject.entity.looselabel_record;
import com.woniu.woniuproject.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 功能描述:<br>
 * 〈〉
 *
 * @author 徐威
 * @create 2020/11/25
 * @since 1.0.0
 */
@Service
public class Looselabel_recordService {
    @Autowired
    looselabel_recordMapper looselabel_recordMappers;
    public List<looselabel_record> queryisReturnedtrue(int i,int userid) {
        return looselabel_recordMappers.selectisReturned(i,userid);
    }

    public looselabel_record queryBykey(Integer id) {
        return looselabel_recordMappers.selectBykey(id);
    }
    @Autowired
    AccountMapper accountMapper;
    @Autowired
    LooseLabelMapper looseLabelMapper;
    @Autowired
    Account_logMapper account_logMapper;
    @Autowired
    moneyCountMapper moneyCountMappers;
    @Transactional
    public int payment(Integer id, Integer id1) {
        Account account = accountMapper.selectByKey(id1);
        looselabel_record looselabel_record = looselabel_recordMappers.selectBykey(id);
        LooseLabel looseLabel = looseLabelMapper.selectBykey(looselabel_record.getClaimsId());
        accountMapper.updateByKey(looselabel_record.getReceivableMoney(),account.getId());//修改个人钱包可用余额
        moneyCountMappers.updateadd(looselabel_record.getReceivableMoney());//更新公司账户
        Account_log account_log = new Account_log();
        account_log.setAuserid(looselabel_record.getClaimsId());
        account_log.setAmainaccountid(1);
        account_log.setAreceiveorpay("收");
        account_log.setAtransferserialno(looseLabel.getProjectnumber());
        account_log.setAdate(new Date());
        account_log.setAtype("散标");
        account_log.setAdescreption("散标还款");
        double money = moneyCountMappers.selectmoney();
        account_log.setAamount(looselabel_record.getReceivableMoney());
        account_log.setAbeforetradingmoney(money);
        account_log.setAaftertradingmoney(money+looselabel_record.getReceivableMoney());
        int o=account_logMapper.sysinsert(account_log);//新增公司交易日志
       int i= looseLabelMapper.updaterepaymentNo(looselabel_record.getClaimsId());//修改散标已还款期数
        looselabel_recordMappers.updateBykey(looselabel_record.getId(),new Date());//修改还款状态
        if (looseLabel.getPeriods()>looselabel_record.getCurrentTerm()){
            Date receivableDate = looselabel_record.getReceivableDate();
            Calendar rightNow = Calendar.getInstance();
            rightNow.setTime(receivableDate);
            rightNow.add(Calendar.MONTH, 1);
            Date dt1 = rightNow.getTime();//还款日
            looselabel_record.setReceivableDate(dt1);
            looselabel_record.setCurrentTerm(looselabel_record.getCurrentTerm()+1);
            looselabel_recordMappers.insert(looselabel_record);//如果还款期数未完  就生成下一期的还款账单
        }
        return  i ;
    }
}
